<template>
  <div class="app-container">
    <Ht-search-form
      v-show="showSearch"
      :dictMap="dictMapAsync"
      :queryParams="queryParams"
      :outputQueryParams="queryParams"
      :formOptions="formOptions"
      :defaultEventKey="defaultEventKey"
      :advOptions="[]"
      :labelWidth="75"
    ></Ht-search-form>

    <!--    <div class="page-label-block">-->
    <!--      <span class="color-block">|</span>-->
    <!--      <span class="page-label-name">-->
    <!--      租户管理-->
    <!--      </span>-->
    <!--    </div>-->

    <ht-table
      ref="dataTable"
      :multi-select="false"
      :showSerialNumber="false"
      :dictMap="dictMap"
      :queryParams="queryParams"
      :tableBindColumns="tableBindColumns"
      :opButtonList="opButtonList"
      :reqConfig="regConfig"
      :defaultEventKey="defaultEventKey"
    >
      <template v-slot:operate>
        <el-table-column label="操作" align="left" width="50" fixed="right">
          <template slot-scope="scope">
            <ht-button-group
              :buttonList="[
                {
                  label: '明细',
                  doFunc: () => {
                    handleAction('detail', scope.row);
                  },
                },
              ]"
            />
          </template>
        </el-table-column>
      </template>
    </ht-table>

    <!-- 添加或修改弹出框 -->
    <el-dialog :title="title" center :visible.sync="open" width="1024px" append-to-body :close-on-click-modal="false">
      <el-form ref="detailForm" :model="detailForm" :rules="rules" label-width="120px">
        <el-card class="form-card txnInfo-card">
        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="交易流水号" prop="id">
              <el-input v-model="detailForm.id" :disabled="true" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="报文标识号" prop="pkgId">
              <el-input v-model="detailForm.pkgId" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="CPG日期" prop="vdate">
              <el-date-picker
                v-model="detailForm.vdate"
                style="width: 100%"
                type="date"
                value-format="yyyyMMdd"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工作日期" prop="workdate">
              <el-date-picker
                v-model="detailForm.workdate"
                style="width: 100%"
                type="date"
                value-format="yyyyMMdd"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="报文类型" prop="pkgNo">
              <el-select
                style="width: 100%"
                v-model="
                  detailForm.pkgNo === null || detailForm.pkgNo === undefined
                    ? ''
                    : detailForm.pkgNo + '-' + detailForm.pkgNoDesc
                "
                placeholder="报文类型"
                :disabled="true"
              ></el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="发起直接参与机构号" prop="directSender">
              <el-input v-model="detailForm.directSender" :disabled="true" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="发起直接参与机构名" prop="directSenderName">
              <el-input v-model="detailForm.directSenderName" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="接收直接参与机构号" prop="directRecver">
              <el-input v-model="detailForm.directRecver" :disabled="true" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="接收直接参与机构名" prop="directRecverName">
              <el-input v-model="detailForm.directRecverName" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="协议查询或调整标识" prop="protoInqRef">
              <el-select
                style="width: 100%"
                v-model="detailForm.protoInqRef"
                placeholder="协议查询或调整标识"
                :disabled="true"
              >
                <el-option
                  v-for="dict in dictMap.PROTO_INQ_REF"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="合同（协议）类型" prop="protoType">
              <el-select
                style="width: 100%"
                v-model="detailForm.protoType"
                placeholder="合同（协议）类型"
                :disabled="true"
              >
                <el-option
                  v-for="dict in dictMap.CONTRACTAGREEMENTTYPECODE"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="协议数目" prop="protoNum">
              <el-input v-model="detailForm.protoNum" :disabled="true" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="通讯状态" prop="comStatus">
              <el-select style="width: 100%" v-model="detailForm.comStatus" placeholder="通讯状态" :disabled="true">
                <el-option
                  v-for="dict in dictMap.PKG_STATUS"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="业务状态" prop="txnStatus">
              <el-select style="width: 100%" v-model="detailForm.txnStatus" placeholder="业务状态" :disabled="true">
                <el-option
                  v-for="dict in dictMap.TXN_STATUS"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="中心状态" prop="procStatus">
              <el-select style="width: 100%" v-model="detailForm.procStatus" placeholder="中心状态" :disabled="true">
                <el-option
                  v-for="dict in dictMap.PROCESSSTATUS"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="报文接收时间" prop="rcvTime">
              <el-date-picker
                style="width: 100%"
                v-model="detailForm.rcvTime"
                value-format="yyyyMMddHHmmss"
                type="datetime"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input type="textarea" v-model="detailForm.remark" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>
          <div class="discountReceipt-pick-operate-box">
            <div class="operate-box-bottom">
              <div class="bottom-content">
                <ht-table
                  ref="dataTabledetail"
                  :multi-select="false"
                  :showSerialNumber="false"
                  :dictMap="dictMap"
                  :queryParams="queryDetailParams"
                  :tableBindColumns="tableBindDetailColumns"
                  :opButtonList="opButtonList"
                  :reqConfig="regConfigDetail"
                  :showRightToolbar="false"
                  :showPagination="false"
                >
                  <template v-slot:operate>
                    <el-table-column label="操作" align="left" width="50" fixed="right">
                      <template slot-scope="scope">
                        <ht-button-group
                          :buttonList="[
                {
                  label: '应 答',
                  permission: '',
                  visible: !(scope.row.bizStatus === undefined ? false : scope.row.bizStatus !== ''),
                  doFunc: () => {
                    replydetial(scope.$index, scope.row);
                  },
                },
              ]"
                        />
                      </template>
                    </el-table-column>
                  </template>
                </ht-table>
              </div>
            </div>
          </div>
        </el-card>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button type="primary" @click="save">提 交</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" center :visible.sync="opendetail" width="1024px" append-to-body :close-on-click-modal="false">
      <el-form ref="detailForm1" :model="detailForm1" :rules="replurules" label-width="120px">
        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="协议号" prop="protoId">
              <el-input v-model="detailForm1.protoId" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="付款人账号" prop="payerActno">
              <el-input v-model="detailForm1.payerActno" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="付款人名称" prop="payerName">
              <el-input v-model="detailForm1.payerName" :disabled="true" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="付款人开户行行号" prop="payerAccBrno">
              <el-input v-model="detailForm1.payerAccBrno" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="业务状态" prop="bizStatus">
              <el-select
                style="width: 100%"
                v-model="detailForm1.bizStatus"
                placeholder="业务状态"
                @change="bizStatusChange"
              >
                <el-option
                  v-for="dict in dictMap.ACTNO_PROC_STATUS"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="付款人账户状态" prop="payerAccStatus">
              <el-select style="width: 100%" v-model="detailForm1.payerAccStatus" placeholder="付款人账户状态">
                <el-option
                  v-for="dict in dictMap.ACCOUNTSTATUSCODE"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="8">
            <el-form-item label="业务拒绝处理码" prop="bizRejectCode">
              <el-input
                v-model="detailForm1.bizRejectCode"
                placeholder="请输入业务拒绝处理码"
                @change="getbase('REJECTCODE')"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-input v-model="detailForm1.bizRejectCodeDesc" :disabled="true" />
          </el-col>
          <el-col :span="4">
            <el-row>
              <el-button type="primary" icon="el-icon-search" plain @click="openBasedataDialog('REJECTCODE')">
                请选择
              </el-button>
            </el-row>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="24">
            <el-form-item label="业务拒绝信息" prop="bizRejectInfo">
              <el-input type="textarea" v-model="detailForm1.bizRejectInfo"  maxlength="105" show-word-limit/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer" align="center">
        <el-button type="primary" @click="replysave(detailForm1.index)">提 交</el-button>
        <el-button type="primary" @click="refresh">重 置</el-button>
      </div>
    </el-dialog>

    <base-data-dialog ref="basedataDialog" @confirm="confirmData"></base-data-dialog>
  </div>
</template>

<script>
import { listPageMixin } from "@/libs/listPageMixin";
import {
  apiUrlConfigDetailQuery,
  apiUrlConfigQuery,
  BatchCustsSignPtlMgtRspEntryDetail,
  BatchCustsSignPtlMgtRspEntrySave,
  datchCustsSignPtlMgtRspEntryDetailQuery
} from "@/api/cnaps/beps/393";

import BaseDataDialog from "@/views/components/BasedataDialog";
import { getBasedata } from "@/api/cnaps/common/basedata";
import { AgentPayInOutApproveApprove } from "@/api/cnaps/beps/392";

export default {
  name: "BatchCustsSignPtlMgtRspEntry",
  mixins: [listPageMixin],
  components: { BaseDataDialog },
  data() {
    return {
      // 查询参数
      queryParams: {
        startworkdate:'',
        endworkdate:'',
        startcpgdate:'',
        endcpgdate:'',
        idSeqno:'',
      },
      queryDetailParams: {},
      detailTableData:[],
      defaultEventKey: "defaultEventKey",
      loading: false,
      // 搜索
      formOptions: [],
      //
      searchFormDictList: [
        "PROTO_INQ_REF",
        "CONTRACTAGREEMENTTYPECODE",
        "PKG_STATUS",
        "TXN_STATUS",
        "PROCESSSTATUS",
        "PROTO_CHANGECODE",
        "ACTNO_PROC_STATUS",
        "ACCOUNTSTATUSCODE",
      ],
      // 列信息
      regConfig: apiUrlConfigQuery,
      regConfigDetail: apiUrlConfigDetailQuery,
      tableBindColumns: [
        { prop: "id", label: "交易流水号" },
        { prop: "pkgId", label: "报文标识号" },
        { prop: "vdate", label: "CPG日期", type: "date" },
        { prop: "workdate", label: "工作日期", type: "date" },
        { prop: "protoType", label: "合同（协议）类型", dictCode: "CONTRACTAGREEMENTTYPECODE" },
        { prop: "protoInqRef", label: "协议查询或调整标识", dictCode: "PROTO_INQ_REF" },
        { prop: "rcvTime", label: "报文接收时间", type: "date" },
      ],

      tableBindDetailColumns: [
        { prop: "protoId", label: "协议号" },
        { prop: "payerActno", label: "付款人账号" },
        { prop: "payerName", label: "付款人名称" },
        { prop: "payerAccBrno", label: "付款人开户行行号", width: 150 },
        { prop: "payeeName", label: "收款人名称" },
        { prop: "chgType", label: "变更类型", dictCode: "PROTO_CHANGECODE" },
        { prop: "bizStatus", label: "业务状态", dictCode: "ACTNO_PROC_STATUS" },
      ],
      dictMapAsync: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      opendetail: false,
      // 表单参数
      detailForm: {
        id: undefined,
        pkgId: undefined,
        tablelist: [],
      },
      detailForm1: {
        protoId: undefined,
        payerActno: undefined,
        payerName: undefined,
        payerAccBrno: undefined,
        bizStatus: undefined,
        payerAccStatus: undefined,
        bizRejectCode: undefined,
        bizRejectCodeDesc: undefined,
        bizRejectInfo: undefined,
      },
      totalRecord: 0,
      totalPage: 0,
      needCount: 0,

      rules: {},

      replurules: {
        protoId: [{ required: true, message: "请输入协议号", trigger: "blur" }],
        bizStatus: [{ required: true, message: "请输入业务状态", trigger: "blur" }],
        bizRejectCode: [{ required: true, message: "请输入业务拒绝处理码", trigger: ['blur', 'change'] }],
      },
    };
  },
  mounted() {
    const formOptions = [
      // 普通搜索

      {
        type: "dates",
        nameObj: {
          startTimeName: "startworkdate",
          endTimeName: "endworkdate",
        },
        defaultAttr: {
          size: "small",
          label: "工作日期",
          placeholder: ["请输入工作开始日期", "请输入工作结束日期"],
          format: "yyyy-MM-dd",
          valueFormat: "yyyyMMdd",
        },
      },

      {
        type: "dates",
        nameObj: {
          startTimeName: "startcpgdate",
          endTimeName: "endcpgdate",
        },
        defaultAttr: {
          size: "small",
          label: "CPG日期",
          placeholder: ["请输入CPG开始日期", "请输入CPG结束日期"],
          format: "yyyy-MM-dd",
          valueFormat: "yyyyMMdd",
        },
      },

      {
        type: "input",
        prop: "idSeqno",
        defaultAttr: {
          size: "small",
          label: "交易流水号",
          placeholder: "请输入交易流水号",
        },
      },
    ];
    this.formOptions = formOptions;
    this.dictMapAsync = this.getDictMap();
  },
  methods: {
    /***
     * 按钮操作汇总
     */
    /***
     * 按钮操作汇总
     */
    handleAction(type, row = {}) {
      switch (type) {
        case "detail":
          this.handleDetail(row);
          break;
      }
    },

    // 关闭按钮
    cancel() {
      this.open = false;
    },
    detailSearch(id) {
      datchCustsSignPtlMgtRspEntryDetailQuery(id).then((res) => {
        this.detailTableData = res.result.data;
      });
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.title = "批量客户签约协议管理应答录入明细";
      BatchCustsSignPtlMgtRspEntryDetail(row).then((response) => {
        this.detailForm = response.result;
        this.queryDetailParams.id = response.result.id;
        this.$nextTick(() => {
          this.$refs.dataTabledetail.doQuery(true);
        });
         this.open = true;
      });
    },

    replydetial(index, row) {
      this.detailForm1 = Object.assign({}, row); //将数据传入dialog页面
      this.detailForm1.index = index; //传递当前index
      this.opendetail = true;
    },

    replysave(index) {
      this.$refs["detailForm1"].validate((valid) => {
        if (valid) {
          this.$refs.dataTabledetail.dataList[index].protoId = this.detailForm1.protoId;
          this.$refs.dataTabledetail.dataList[index].payerActno = this.detailForm1.payerActno;
          this.$refs.dataTabledetail.dataList[index].payerName = this.detailForm1.payerName;
          this.$refs.dataTabledetail.dataList[index].payerAccBrno = this.detailForm1.payerAccBrno;
          this.$refs.dataTabledetail.dataList[index].bizStatus = this.detailForm1.bizStatus;
          this.$refs.dataTabledetail.dataList[index].payerAccStatus = this.detailForm1.payerAccStatus;
          this.$refs.dataTabledetail.dataList[index].bizRejectCode = this.detailForm1.bizRejectCode;
          this.$refs.dataTabledetail.dataList[index].bizRejectCodeDesc = this.detailForm1.bizRejectCodeDesc;
          this.$refs.dataTabledetail.dataList[index].bizRejectInfo = this.detailForm1.bizRejectInfo;
          // this.$refs.dataTabledetail.initColPer(this.$refs.dataTabledetail.dataList);
          // 重新设置到tableData中，刷新表格数据
          var value = this.$refs.dataTabledetail.dataList[index];
          this.$set(this.$refs.dataTabledetail.dataList, index, value);
          this.opendetail = false;
        }
      });
    },

    openBasedataDialog(dataType = "") {
      this.$refs.basedataDialog.show(dataType);
    },

    confirmData(fromFlag, row) {
      this.$set(this.detailForm1, "bizRejectCode", row.dataValue);
      this.$set(this.detailForm1, "bizRejectCodeDesc", row.dataDesc);
    },
    //回车获取basedata值
    getbase(dataType) {
      const dataValue = this.detailForm1.bizRejectCode;
      getBasedata(dataType, dataValue)
        .then((response) => {
          if (response.code == "S" && response.result !== null && response.result !== undefined) {
            this.$set(this.detailForm1, "bizRejectCode", response.result.dataValue);
            this.$set(this.detailForm1, "bizRejectCodeDesc", response.result.dataDesc);
          } else {
            this.msgSuccess("找不到对应的字典项");
            this.$set(this.detailForm1, "bizRejectCode", null);
            this.$set(this.detailForm1, "bizRejectCodeDesc", null);
          }
        })
        .catch((err) => {
          this.$set(this.detailForm1, "bizRejectCode", null);
          this.$set(this.detailForm1, "bizRejectCodeDesc", null);
        });
    },

    bizStatusChange() {
      if (this.detailForm1.bizStatus === "PR05") {
        this.$set(this.replurules.bizRejectCode[0], "required", false);
      } else {
        this.$set(this.replurules.bizRejectCode[0], "required", true);
      }
    },

    //重置按钮
    refresh() {
      this.resetForm("detailForm1");
      this.$set(this, "detailForm1", {});
    },

    save() {
      this.detailForm.tablelist = this.$refs.dataTabledetail.dataList;
      this.$refs["detailForm"].validate((valid) => {
        if (valid) {
          BatchCustsSignPtlMgtRspEntrySave(this.detailForm)
            .then((res) => {
              this.loading = true;
              this.msgSuccess("提交成功");
              this.open = false;
              this.loading = false;
              this.$alert(`CORE_ID: ${res.result.rspId}`, "生成交易流水号", {
                confirmButtonText: "确定",
                callback: (action) => {
                },
              });
              this.$nextTick(() => {
                this.$refs.dataTable.doQuery(true);
              });
            })
            .catch((err) => {
              this.loading = false;
            });
        }
      });
    },
  },
};
</script>
